using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using SharpCore.Data;

namespace TF.Access {
	public partial class TF_UserProfile {

        private bool hasPassword = true;

        public bool HasPassword
        {
            get
            {
                return hasPassword;
            }
            set
            {
                hasPassword = value;
            }
        }

		#region Methods
		
		/// <summary>
		/// Selects a single record from the TF_UserProfile table.
		/// </summary>
		public static TF_UserProfile Login(string email, string pwd) {
            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader("TF_UserProfile_Login", email, pwd))
            {
				if (dataReader.Read()) {
					return MakeTF_UserProfile(dataReader);
				} else {
					return null;
				}
			}
		}

        /// <summary>
        /// Selects a single record from the TF_UserProfile table.
        /// </summary>
        public static TF_UserProfile Login(string email)
        {
            TF_UserProfile ret = null;
            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader("TF_UserProfile_LoginByEmail", email))
            {
                if (dataReader.Read())
                {
                    ret = MakeTF_UserProfile(dataReader);
                    ret.HasPassword = false;
                }
                else
                {
                    ret = null;
                }
            }

            return ret;
        }

        /// <summary>
        /// Selects top 9
        /// </summary>
        public static List<TF_UserProfile> SelectNewFriends()
        {
            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader("TF_UserProfileSelectNewFriends",9,1))
            {
                List<TF_UserProfile> tF_UserProfileList = new List<TF_UserProfile>();
                while (dataReader.Read())
                {
                    TF_UserProfile tF_UserProfile = MakeTF_UserProfile(dataReader);
                    tF_UserProfileList.Add(tF_UserProfile);
                }
                return tF_UserProfileList;
            }
        }

        /// <summary>
        /// -1: can reg;0:duplicate email;1:duplicate nick_name
        /// </summary>
        /// <param name="email"></param>
        /// <param name="nickName"></param>
        /// <returns></returns>
        public static int UserProfileRegCheck(TF_UserProfile up)
        {
            int ret = -1;
            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader("TF_UserProfile_Reg_Check", up.Email, up.Nick_Name))
            {
                if (dataReader.Read())
                {
                    if (!dataReader.IsDBNull(0) &&
                        !dataReader.IsDBNull(1))
                    {
                        int cont = dataReader.GetInt32(0);
                        if (cont > 0)
                        {
                            ret = dataReader.GetInt32(1);
                            return ret;
                        }
                    }
                }

                while (dataReader.NextResult())
                {
                    if (dataReader.Read())
                    {
                        if (!dataReader.IsDBNull(0) &&
                            !dataReader.IsDBNull(1))
                        {
                            int cont = dataReader.GetInt32(0);
                            if (cont > 0)
                            {
                                ret = dataReader.GetInt32(1);
                                break;
                            }
                        }
                    }
                }
            }

            return ret;
        }
		#endregion
	}
}
